package com.bingxue.edu.management.system.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.job.QuartzSchedulerService;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.system.model.entity.ScheduleJob;
import com.bingxue.edu.management.system.model.query.ScheduleJobLogQuery;
import com.bingxue.edu.management.system.model.query.ScheduleJobQuery;
import com.bingxue.edu.management.system.model.req.ScheduleJobReq;
import com.bingxue.edu.management.system.model.resp.ScheduleJobDetailResp;
import com.bingxue.edu.management.system.model.resp.ScheduleJobLogDetailResp;
import com.bingxue.edu.management.system.model.resp.ScheduleJobLogResp;
import com.bingxue.edu.management.system.model.resp.ScheduleJobResp;
import com.bingxue.edu.management.system.service.ScheduleJobLogService;
import com.bingxue.edu.management.system.service.ScheduleJobService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.bingxue.edu.management.system.model.entity.table.ScheduleJobLogTableDef.SCHEDULE_JOB_LOG;

/**
 * 定时任务接口
 *
 * @author panbo
 * @since 2024/08/02 13:34
 */
@Tag(name = "定时任务接口")
@RestController
@RequestMapping("/system/job")
@RequiredArgsConstructor
public class ScheduleJobController {

    private final ScheduleJobService scheduleJobService;
    private final QuartzSchedulerService quartzSchedulerService;
    private final ScheduleJobLogService scheduleJobLogService;

    @Operation(summary = "查询列表")
    @ApiResource("system:schedule:manage")
    @GetMapping("/list/job-beans")
    public Response<Object> listJobBeans() {
        List<Map<String, Object>> jobs = quartzSchedulerService.loadAllJobs();
        return Response.success(jobs);
    }

    @Operation(summary = "分页查询列表")
    @ApiResource("system:schedule:manage")
    @GetMapping("/page")
    public Response<Pagination<ScheduleJobResp>> page(ScheduleJobQuery query, @Validated PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        Page<ScheduleJobResp> page = scheduleJobService.pageAs(pageQuery.toPage(), queryWrapper, ScheduleJobResp.class);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询详情")
    @ApiResource("system:schedule:manage")
    @GetMapping("/detail/{id}")
    public Response<ScheduleJobDetailResp> detail(@PathVariable String id) {
        ScheduleJobDetailResp detail = scheduleJobService.getMapper().selectOneWithRelationsByIdAs(id, ScheduleJobDetailResp.class);
        return Response.success(detail);
    }

    @Operation(summary="创建数据")
    @ApiResource("system:schedule:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated ScheduleJobReq req) {
        ScheduleJob scheduleJob = scheduleJobService.copy(req);
        scheduleJobService.createJob(scheduleJob);
        return Response.success();
    }

    @Operation(summary="更新数据")
    @ApiResource("system:schedule:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated ScheduleJobReq req, @PathVariable String id) {
        ScheduleJob scheduleJob = scheduleJobService.copy(req);
        scheduleJob.setId(id);
        scheduleJobService.updateJob(scheduleJob);
        return Response.success();
    }

    @Operation(summary="删除数据")
    @ApiResource("system:schedule:manage")
    @PostMapping("/delete/{id}")
    public Response<Void> delete(@PathVariable String id) {
        scheduleJobService.deleteJob(id);
        return Response.success();
    }

    @Operation(summary="立即执行一次")
    @ApiResource("system:schedule:manage")
    @PostMapping("/execute/{jobId}")
    public Response<Void> deleteLog(@PathVariable String jobId) {
        scheduleJobService.executeOnceJob(jobId);
        return Response.success();
    }

    @Operation(summary = "分页查询任务日志")
    @ApiResource("system:log:manage")
    @GetMapping("/log/page")
    public Response<Pagination<ScheduleJobLogResp>> getLogPage(ScheduleJobLogQuery query, @Validated PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        queryWrapper.select(SCHEDULE_JOB_LOG.DEFAULT_COLUMNS);
        Page<ScheduleJobLogResp> page = scheduleJobLogService.pageAs(pageQuery.toPage(), queryWrapper, ScheduleJobLogResp.class);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询日志详情")
    @ApiResource("system:log:manage")
    @GetMapping("/log/detail/{id}")
    public Response<ScheduleJobLogDetailResp>  getLogDetail(@PathVariable String id) {
        ScheduleJobLogDetailResp detail = scheduleJobLogService.getMapper().selectOneWithRelationsByIdAs(id, ScheduleJobLogDetailResp.class);
        return Response.success(detail);
    }


    @Operation(summary="删除任务日志")
    @ApiResource("system:log:manage")
    @PostMapping("/log/delete/{ids}")
    public Response<Void> deleteLog(@PathVariable List<String> ids) {
        scheduleJobLogService.removeByIds(ids);
        return Response.success();
    }

}
